package cn.edu.nju.software.service.impl;

import java.sql.SQLException;
import java.util.List;

import cn.edu.nju.software.bean.storage.Admin;
import cn.edu.nju.software.bean.storage.User;
import cn.edu.nju.software.dao.intf.AdminDao;
import cn.edu.nju.software.dao.intf.UserDao;
import cn.edu.nju.software.service.intf.AdminService;
import cn.edu.nju.software.util.Constants;

public class AdminServiceImpl implements AdminService {
	
	private AdminDao adminDao;
	private UserDao userDao;

	public void setAdminDao(AdminDao adminDao) {
		this.adminDao = adminDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public Admin login(String signInName, String password) throws SQLException {
		return adminDao.login(signInName, password);
	}

	@Override
	public void modifyAdminInfo(Admin admin) throws SQLException {
		adminDao.updateAdmin(admin);
	}

	@Override
	public List<User> searchUsers(int key, String value) throws SQLException {
		switch (key) {
		case Constants.SEARCH_SIGNINNAME:
			return userDao.getUsersBySignInName(value);
		case Constants.SEARCH_NAME:
			return userDao.getUsersByName(value);
		default:
			return userDao.getAllUsers();
				
		}
		
	}

	@Override
	public void operateUser(String signInName, int operateCode) throws SQLException {
		switch (operateCode) {
		case Constants.USER_NORMAL:
			userDao.unblockUser(signInName);
			break;
		case Constants.USER_BLOCKED:
			userDao.blockUser(signInName);
			break;
		case Constants.USER_DELETED:
			userDao.deleteUser(signInName);
			break;
		}
	}

}
